package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveOrUpdate(User user) {
        if (user.getId() == null) {
            User temp = userMapper.queryByEmailAndPassword(user.getEmail(), null);
            if (temp != null) {
                throw new RuntimeException("该邮箱已被注册");
            }
            user.setCoverImgUrl("/img/user/bg.jpeg");
            user.setHeadImgUrl("/img/head.jpg");
            userMapper.insert(user);
        }else{
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.queryByEmailAndPassword(email, password);
        if (user == null) {
            throw new RuntimeException("账号与密码不匹配");
        }
        return user;
    }

    @Override
    public PageInfo<User> list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public User getAuthorInfoById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertRelations(Long id, Long followerId) {
        userMapper.insertRelations(id,followerId);
    }

    @Override
    public int queryNums(Long userId) {
        return userMapper.getNums(userId);
    }

    @Override
    public int queryFans(Long userId) {
        return userMapper.getFans(userId);
    }

    @Override
    public void deleteRelations(Long id, Long followerId) {
        userMapper.deleteRelations(id,followerId);
    }

    @Override
    public Boolean getIsRelation(Long userId, Long followerId) {
        String s = userMapper.selectIsRelation(userId, followerId);
        if(s==null){
            return false;
        }
        return true;
    }

}
